import React, { memo, useCallback } from 'react';
import { Button, message, Modal } from 'antd';

import { salaryRecheck } from '@/services/salaryManage/salaryCalculate';

import { goToProcessApply } from '@/pages/SalaryManage/utils/pageNavigateUtil';
import { PROCESS_TYPE } from '@/pages/SalaryManage/utils/processUtil';

// 提交审批
const ApproveSubmit = memo(({ handleItem, period, organizationCode, searchModel }) => {
    const handleFn = useCallback(async () => {
        Modal.confirm({
            title: '确定提交薪资审批？',
            centered: true,
            onOk: async () => {
                const {
                    contrastType = 1,
                    downAbnormalTipIndex,
                    upAbnormalTipIndex,
                    salaryRateKpiInd = 1
                } = searchModel;
                try {
                    await salaryRecheck
                        .saveApproveBusinessData({
                            year: period.substr(0, 4),
                            month: period.substr(5),
                            contrastType,
                            downAbnormalTipIndex,
                            upAbnormalTipIndex,
                            salaryRateKpiInd,
                            organizationCode
                        })
                        .then(res => {
                            goToProcessApply(
                                PROCESS_TYPE.SALARY_RECHECK_APPLY,
                                { contrastType, period, organizationCode, salaryPayrollProcessRecordId: res.data },
                                '没有找到分类为【财务薪资复核审批流程】的流程，请先配置流程'
                            );
                        });
                } catch (error) {
                    message.warn('提交失败');
                }
            }
        });
    }, [period, organizationCode, searchModel]);


    return (
        <Button key={handleItem.code} onClick={handleFn}>
            {handleItem.name}
        </Button>
    );
});

export default memo(ApproveSubmit)
